package xyz.wpxy.mqttdemo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.wpxy.mqttdemo.config.mqtt.MqttOutBoundConfiguration;
import xyz.wpxy.mqttdemo.domain.MqttProperties;
import xyz.wpxy.mqttdemo.domain.vo.MyMessage;
import xyz.wpxy.mqttdemo.service.MqttGateway;

/**
 * @Description:
 * @author: Peng Wei
 * @date:2022/11/24 11:40
 */
@RestController
@AllArgsConstructor
@RequestMapping("/test")
@Api("MQTT测试")
public class MqttController {

    private final MqttProperties mqttProperties;

    private final MqttGateway mqttGateway;


    @GetMapping("/mqttDemo")
    @ApiOperation("MQTT接口测试")
    public String mqttDemo() {
        System.out.println(mqttProperties);
        return "你好,测试mqtt";
    }

    @PostMapping("/send")
    @ApiOperation("MQTT发送数据")
    public String send(@RequestBody MyMessage myMessage) {
        // 发送消息到指定主题
        mqttGateway.sendToMqtt(myMessage.getTopic(), 2, myMessage.getContent());
        return "send topic: " + myMessage.getTopic() + ", message : " + myMessage.getContent();
    }
}
